package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.base.Header;
import cc.rengu.igas.bsps.facade.request.GetCommonPublicKeyRequest;
import cc.rengu.igas.bsps.facade.response.GetCommonPublicKeyResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.CertInfoMapper;
import cc.rengu.oltp.service.common.dao.SecPlanInfoMapper;
import cc.rengu.oltp.service.common.dao.SrcChannelInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.CertInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.SecPlanInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.SrcChannelInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.CertInfo;
import cc.rengu.oltp.service.common.entity.SecPlanInfo;
import cc.rengu.oltp.service.common.entity.SrcChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.RedisUtil;
import com.alibaba.fastjson.JSON;

/**
 * 获取通讯公钥 on 2020/2/25.
 */
public class GetCommonPublicKeyService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        GetCommonPublicKeyRequest request = new GetCommonPublicKeyRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        GetCommonPublicKeyRequest getCommonPublicKeyRequest = (GetCommonPublicKeyRequest) request;

        Header header = getCommonPublicKeyRequest.getHeader();

        //通过Redis获取交易渠道信息
        String srcChannelInfoKey = header.getInstId().trim() + header.getSrcSysId().trim() + header.getChanlId().trim();
        SrcChannelInfo srcChannelInfo = JSON.parseObject(RedisUtil.hashGet(AppParamConstant.CHANNEL_ACQ_INFO_CACHE, srcChannelInfoKey), SrcChannelInfo.class);
        if (null == srcChannelInfo) {
            srcChannelInfoKey = header.getInstId().trim() + header.getSrcSysId().trim() + "*";
            srcChannelInfo = JSON.parseObject(RedisUtil.hashGet(AppParamConstant.CHANNEL_ACQ_INFO_CACHE, srcChannelInfoKey), SrcChannelInfo.class);
            if (null == srcChannelInfo) {
                SrcChannelInfoMapper srcChannelInfoMapper = new SrcChannelInfoMapperImpl();
                srcChannelInfo = srcChannelInfoMapper.selectSrcChannelInfoByPrimaryKey(txnInfo.getSrcTxnAuthCfg().getInstId().trim(), txnInfo.getSrcTxnAuthCfg().getMsgSrcId().trim(), txnInfo.getSrcTxnAuthCfg().getTransChannelId().trim());
            }
        }
        if (null == srcChannelInfo) {
            rglog.error("交易渠道<{}>的细分渠道<{}>未配置或已停用！", txnInfo.getSrcTxnAuthCfg().getMsgSrcId(), txnInfo.getSrcTxnAuthCfg().getTransChannelId());
            throw new BizException(OltpRpcdEnum.CHANNEL_DISABLED);
        }

        //安全标识
        String secPlanId = srcChannelInfo.getSecPlanId();

        SecPlanInfoMapper secPlanInfoMapper = new SecPlanInfoMapperImpl();

        SecPlanInfo secPlanInfo = secPlanInfoMapper.selectSecPlanInfoByPrimaryKey(header.getInstId(), secPlanId, BspsAppParamConstant.PUBLIC_SIGN_CERT_TYPE);
        if (null == secPlanInfo) {
            rglog.error("安全计划查询为空！instId:<{}>,secPlanId:<{}>,secPlanType:<{}>", header.getInstId(),
                    secPlanId, BspsAppParamConstant.PUBLIC_SIGN_CERT_TYPE);
            throw new BizException(RespCodeEnum.RECORD_NOT_FOUND.getRespCode(), "机构安全计划查询为空");
        } else if (AppParamConstant.NO.equals(secPlanInfo.getSecPlanStatus())) {
            rglog.error("安全计划状态已失效！instId:<{}>,secPlanId:<{}>,secPlanType:<{}>,状态:<{}>", header.getInstId(),
                    secPlanId, BspsAppParamConstant.PUBLIC_SIGN_CERT_TYPE, secPlanInfo.getSecPlanStatus());
            throw new BizException(RespCodeEnum.RECORD_NOT_FOUND.getRespCode(), "机构安全计划状态失效");
        }

        //查询证书表
        CertInfoMapper certInfoMapper = new CertInfoMapperImpl();
        String certIndex = secPlanInfo.getInstId() + secPlanInfo.getSecPlanId() + secPlanInfo.getSecPlanType();
        CertInfo certInfo = certInfoMapper.selectCertInfoByPrimaryKey(certIndex);
        if (null == certInfo) {
            rglog.error("机构公钥证书查询为空！certIndex:<{}>", certIndex);
            throw new BizException(RespCodeEnum.RECORD_NOT_FOUND.getRespCode(), "机构公钥证书查询为空");
        } else if (AppParamConstant.NO.equals(certInfo.getKeyStatus())) {
            rglog.error("机构公钥证书查询证书状态失效！certIndex:<{}>", certIndex);
            throw new BizException(RespCodeEnum.QUERY_STATUS_ERROR.getRespCode(), "机构公钥证书状态已失效");
        }

        BizResponse<GetCommonPublicKeyResponse> bizResponse = new BizResponse<>();
        GetCommonPublicKeyResponse response = new GetCommonPublicKeyResponse();
        ConvertUtil.convertOutput(response);
        response.setPublicKey(certInfo.getCertValue());
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(response);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GetCommonPublicKeyResponse getCommonPublicKeyResponse = (GetCommonPublicKeyResponse) bizResponse.getResult();
        ConvertUtil.convertInput(getCommonPublicKeyResponse);
    }
}
